<template>
  <div class="app-container">
    <el-card class="box-card">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"   >
      <el-form-item label="栏目" prop="contentColumn">
        <el-select
          v-model="queryParams.contentColumn"
          placeholder="请选择栏目"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zhiliang"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="noticeTitle" >
        <el-input
          v-model="queryParams.noticeTitle"
          placeholder="请输入质量成果标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="图片路径" prop="pictureUrl">
        <el-input
          v-model="queryParams.pictureUrl"
          placeholder="请输入图片路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材料路径" prop="sourcesUrl">
        <el-input
          v-model="queryParams.sourcesUrl"
          placeholder="请输入材料路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="发布时间" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>

      <br/>
      <div style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
      <br/>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dr:content:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dr:content:edit']"
        >修改</el-button>
      </el-col>-->

      <el-col :span="1.5">

        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dr:content:remove']"
        >删除</el-button>

      </el-col>



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dr:content:export']"
        >导出</el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList"  border @selection-change="handleSelectionChange">
      <el-table-column type="selection"  align="center" />
      <el-table-column type="index" label="序号"   :index="indexMethod" align="center"/>
      <el-table-column label="栏目" align="center" prop="contentColumn" >
      <template slot-scope="scope">
        <dict-tag :options="dict.type.zhiliang" :value="scope.row.contentColumn"/>
      </template>
      </el-table-column>
      <el-table-column label="标题" width="500" align="center" prop="noticeTitle"  />
      <el-table-column label="发布时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dr:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dr:content:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon=""
            @click="handleView(scope.row)"
            v-hasPermi="['dr:content:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改内容发布对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-container>
          <el-main>
            <el-row>
              <el-col span="12" style="display: none">
                <el-form-item label="类型" prop="noticeType">
                  <el-input v-model="form.noticeType='z'"></el-input>

                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="栏目" prop="contentColumn">
                  <el-select
                    v-model="form.contentColumn"
                    placeholder="请输入栏目名"
                    clearable
                    style="width: 100%"
                  >
                    <el-option
                      v-for="dict in dict.type.zhiliang"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"/>
                  </el-select>

                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="标题" prop="noticeTitle">
                  <el-input style="width: 100%" v-model="form.noticeTitle" placeholder="请输入标题" />

                </el-form-item>
              </el-col>

            </el-row>
            <el-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192" placeholder="请输入内容"/>

            </el-form-item>


<!--            <el-row>
              <el-col span="12">

                <el-form-item label="资料路径" prop="note1">
                  <file-upload v-model="form.note1"/>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="图片路径" prop="note2">
                  <image-upload v-model="form.note2"/>

                </el-form-item>
              </el-col>
            </el-row>-->
            <el-row>
              <el-col :span="12">
                <el-form-item label="状态" prop="noticeStatus" >

                  <el-select
                    v-model="form.noticeStatus"
                    placeholder="状态"
                    clearable
                    style="width: 100%"
                  >
                    <el-option
                      v-for="dict in dict.type.yh_notice_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <!-- &lt;!&ndash; 文件目录树 &ndash;&gt;
             <el-tree
               :data="fileTree"
               :props="{
           children: 'children',
           label: 'label'
         }"
               :highlight-current="true"
               :expand-on-click-node="false"
               :default-expanded-keys="defaultExpandedKeys"
               node-key="id"
               v-loading="loading"

             >

             </el-tree>-->

            <!--  <el-form-item label="参与人员" prop="participants">
                <el-input v-model="form.participants" placeholder="请输入参与人员" />
              </el-form-item>
              <el-form-item label="浏览次数" prop="viewsTimes">
                <el-input v-model="form.viewsTimes" placeholder="请输入浏览次数" />
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>-->
            <!-- <el-form-item label="备注1" prop="note1">
               <el-input v-model="form.note1" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注2" prop="note2">
               <el-input v-model="form.note2" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注3" prop="note3">
               <el-input v-model="form.note3" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注4" prop="note4">
               <el-input v-model="form.note4" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注5" prop="note5">
               <el-input v-model="form.note5" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注6" prop="note6">
               <el-input v-model="form.note6" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注7" prop="note7">
               <el-input v-model="form.note7" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注8" prop="note8">
               <el-input v-model="form.note8" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注9" prop="note9">
               <el-input v-model="form.note9" type="textarea" placeholder="请输入内容" />
             </el-form-item>
             <el-form-item label="备注10" prop="note10">
               <el-input v-model="form.note10" type="textarea" placeholder="请输入内容" />
             </el-form-item>-->
<!--
            <el-row v-if="form.isPublic == '1'">
              <el-col :span="12" >
                <el-form-item label="是否公开" prop="isPublic">
                  <el-radio-group v-model="form.isPublic" @change = "changePublic">
                    <el-radio label="1">个人</el-radio>
                    <el-radio label="2">部门</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="12" >
                <el-form-item label="个人通知" prop="note3">
                  <el-select v-model="form.note3" multiple placeholder="请选择" style="width: 100%">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

            </el-row>
            <el-row v-if="form.isPublic == '2'">
              <el-col :span="12" >
                <el-form-item label="是否公开" prop="isPublic" >
                  <el-radio-group v-model="form.isPublic" @change = "changePublic">
                    <el-radio label="1">个人</el-radio>
                    <el-radio label="2">部门</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="12" >


                <el-form-item label="部门通知" prop="note6">
                  <treeselect v-model="form.note6" :options="options1" :show-count="true" clearable

                              :multiple="multiple"
                              placeholder="请选择归属部门"/>
                </el-form-item>
              </el-col>
            </el-row>
-->

          </el-main>
        </el-container>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.ql-toolbar.ql-snow {
  text-align: left;
  border: 1px solid #ccc;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  padding: 8px;
  /* float: none; */
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #FFFFFF;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

/*对话框标题样式*/
.el-dialog__title{
  /*color: #1ab394;*/
  font-weight: bold;
  font-size: 25px;
}
</style>
<script>
import { listContent, getContent, delContent, addContent, updateContent } from "@/api/module/dr/content";
import watermark from "watermark-dom";
import {listDept} from "@/api/system/dept";

import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listUser} from "@/api/system/user";

export default {
  components: {Treeselect},
  name: "Content",
  dicts:['yh_notice_type','yh_notice_status','zhiliang'],
  data() {
    return {
      options1: [],
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 内容公告发布表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeType: 'z',
        noticeTitle: null,
        noticeContent: null,
        noticeStatus: null,
        pictureUrl: null,
        sourcesUrl: null,
        releaseTime: null,
        participants: null,
        viewsTimes: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeType: [
          { required: true, message: "类型不能为空", trigger: "blur" },
        ],
        noticeTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        noticeContent: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        releaseTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" },
        ],
        noticeStatus: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    //this.getUser();
    this.getTreeselect();
    //this.getDept();
  },
  mounted() {
    // 加载水印

    const username = this.$store.state.user.nickname;
    watermark.load({ watermark_txt: ' ' });



  },
  methods: {
    getDept() {
      listDept().then(response => {
        let data = response.data;
        console.log(data + "999")
        /*for (let i = 0; i < data.length; i++) {
          this.options1.push({
           value: data[i].deptId,
           label: data[i].deptName,
         })
        }*/
      });
    },
    //下拉列表获取所有用户信息
    getUser() {
      listUser().then(response => {
        let data = response.rows;
        console.log(data + "899")
        for (let i = 0; i < data.length; i++) {
          this.options.push({
            value: data[i].userName,
            label: data[i].nickName,
          })
        }
      });
    },
    /** 提交按钮 */
    getTreeselect() {
      treeselect().then(response => {
        this.options1 = response.data;
        console.log(this.options1 + "456")
      });
    },
    indexMethod(index) {
      index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return index
    },
    /** 查询内容公告发布列表 */
    getList() {
      this.loading = true;

      listContent(this.queryParams).then(response => {

        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        noticeType: 'z',
        noticeTitle: null,
        noticeContent: null,
        noticeStatus: null,
        pictureUrl: null,
        sourcesUrl: null,
        releaseTime: null,
        participants: null,
        viewsTimes: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null,
        //是否显示
        isPublic: "1",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.is = selection.map(item => item.noticeTitle)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "质量成果";
    },
    /** 提交按钮 */
    changePublic(val) {
      if (val == "1") {

        //清空
        this.form.note6 = null
      }
      if (val == "2") {
        this.form.note3 = null
      }

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改质量成果";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        console.log(this.form.note6 + 'yzx')
        if (valid) {
          if (this.form.id != undefined) {
            //后台接收的数据是string类型，前台传值需要把数组类型的数据转换成string类型
            if (this.form.note3 != undefined) {
              this.form.note3 = this.form.note3.toString()
            }
            /*if (this.form.note6 != undefined) {
              for (i = 0; i < form.note6.length; i++) {
                this.form.note6 = this.form.note6[i].toString()
              }

            }*/
            if (this.form.note6 != undefined) {
              this.form.note6 = this.form.note6.toString()
            }
            if (this.form.noticeContent != null) {
              this.form.noticeContent=this.form.noticeContent.slice(3,-4)
            }
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.form.noticeContent != null) {
              this.form.noticeContent=this.form.noticeContent.slice(3,-4)
            }
            //后台接收的数据是string类型，前台传值需要把数组类型的数据转换成string类型
            if (this.form.note3 != undefined) {
              this.form.note3 = this.form.note3.toString()
            }
            /* if (this.form.note6 != undefined) {
               this.form.note6 = this.form.note6.toString()
             }*/
            // this.form.note6=this.form.deptId.toString();
            /*if (this.form.note6 != undefined) {
              for (i = 0; i < form.note6.length; i++) {
                this.form.note6 = this.form.note6[i].toString()
              }

            }*/
            if (this.form.note6 != undefined) {
              this.form.note6 = this.form.note6.toString()
            }
            console.log(this.form.deptId + "woshi")
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              console.log(this.form.note6 + "11111111");
            });
          }
        }
      });
    },
    handleView(row) {
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.$router.push({ path:"/yewu/drindexxiangqing", query: {id}});
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const i=row.noticeTitle||this.is;
      this.$modal.confirm('是否确认删除标题为"' + i + '"的质量成果？').then(function() {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dr/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
